package com.cjbdi.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.cjbdi.entity.Report;
import com.cjbdi.pagination.PageInfo;
import com.cjbdi.service.ReportService;
import com.cjbdi.service.TFypzService;
import com.cjbdi.util.MD5Util;
import com.cjbdi.util.NumUtil;
import com.cjbdi.util.PropertiesUtil;
import com.cjbdi.util.StringEditor;
import com.cjbdi.vo.Result;

@Controller
@RequestMapping("report")
public class ReportController {
	static final Logger log = LoggerFactory.getLogger(ReportController.class);
	@Autowired
    private ReportService reportService;
	
    @RequestMapping(value = "/save", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Result save(@RequestParam("file") MultipartFile file,String title,String fymc,String fybm,String type,String zsType,String year,String timeDimension) {
    	List<Report> list = reportService.findByTitle(title);
    	if(list.size()>0) {
    		return new Result(801,"报告已经存在",null);
    	}
    	if (file.isEmpty()) {
    		return new Result(808,"文件为空",null);
    	}else {
    		uploadFile(file,title);
        	Report report = new Report();
        	report.setFybm(fybm);
    		report.setFymc(fymc);
    		report.setFybm(fybm.startsWith(";")?fybm.substring(1):fybm);
    		report.setFymc(fymc.startsWith(";")?fymc.substring(1):fymc);
        	report.setTitle(title);
        	report.setCount(0);
        	report.setType(type);
        	report.setZsType(zsType);
        	report.setYear(year);
        	report.setProduceType("upload");
        	report.setTimeDimension(timeDimension);
        	//report.setPath(path);
        	report.setUpdateTime(NumUtil.GetNowDate("yyyy-MM-dd HH:mm:ss"));
        	fybm = new StringEditor(fybm).trimAll().toString();
        	/*if(fybm.substring(1).equals("00")) {
        		report.setFytype("高");
        	}else if(fybm.substring(2).equals("0")) {
        		report.setFytype("中");
        	}else {
        		report.setFytype("辖");
        	}*/
        	reportService.save(report);
        	return new Result(200,"新增报告成功",null);
    	}
    }
    
    public static void uploadFile(MultipartFile file,String title) {
    	String filePath = PropertiesUtil.getProperty("reportPath");
    	//String filePath = Class.class.getClass().getResource("/").getPath()+"/templates/report/";
        //文件名称（默认new Date().getTime()）
        //String fileName = new Date().getTime() + file.getOriginalFilename().split("\\.")[0];
        //String md5Name = MD5Util.GetMD5Code(fileName)+"."+file.getOriginalFilename().split("\\.")[1];
    	//String fileName = file.getOriginalFilename().split("\\.")[0];
    	//String suffix = file.getOriginalFilename().split("\\.")[1];
    	File file_ = new File(filePath);
        if (!file_.exists()) {
            file_.mkdirs();
        }
        BufferedOutputStream stream = null;
        try {
            stream = new BufferedOutputStream(new FileOutputStream(filePath + title + ".pdf"));
            stream.write(file.getBytes());
            stream.flush();
        } catch (IOException e) {
            e.printStackTrace();
            log.error(e.getMessage());
        } finally {
            try {
                if (stream != null) stream.close();
            } catch (IOException e) {
                e.printStackTrace();
                log.error(e.getMessage());
            }
        }
        //return md5Name;
    }
    
    @CrossOrigin
    @RequestMapping(value = "/download", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public static void download(HttpServletResponse response,String path,String title) {
    	String filePath = PropertiesUtil.getProperty("reportPath")+title+".pdf";
    	File f=new File(filePath); 
        if (!f.exists()) {
        	log.info("源文件没有找到，下载失败！");
        }
        try {
	        response.setHeader("content-disposition","attachment;fileName="+java.net.URLEncoder.encode(title)+".pdf");
	        InputStream in = new FileInputStream(filePath); //获取下载文件的输入流
	        int count =0;
	        byte[] by = new byte[1024];
	        OutputStream out = response.getOutputStream();
	        while((count=in.read(by))!=-1){
	        	out.write(by, 0, count);//将缓冲区的数据输出到浏览器
	        }
	        in.close();
	        out.flush();
	        out.close();
	    }catch (IOException e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }
    
    public static void deleteFile(String title) {
    	//String filePath = Class.class.getClass().getResource("/").getPath()+"/templates/report/"+name;
    	String filePath = PropertiesUtil.getProperty("reportPath")+ title +".pdf";
    	File file = new File(filePath);
    	file.delete();
    }

    @RequestMapping(value = "/edit", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Result edit(int id) {
    	List<Report> list = reportService.findById(id);
    	if(list.size()>0) {
    		return new Result(200,"获取报告信息成功",list.get(0));
    	}else {
        	return new Result(802,"获取报告信息失败",null);

    	}
    	
    }
    @RequestMapping(value = "/update", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
	public Result update(@RequestParam(value = "file",required=false) MultipartFile file,String title,String fymc,String fybm,String type,String zsType,String year,String timeDimension,int id) {
    	List<Report> list = reportService.findById(id);
    	if(list.size()>0) {
    		Report report = list.get(0); 
    		if(!report.getTitle().equals(title)) {//修改报告名称需重命名
    			if(reportService.findByTitle(title).size()>0) {
    	    		return new Result(809,"报告已经存在",null);
    	    	}
    			File oldfile = new File(PropertiesUtil.getProperty("reportPath")+ report.getTitle() +".pdf");
    	    	File newfile = new File(PropertiesUtil.getProperty("reportPath")+ title +".pdf");
    	    	if(!oldfile.exists()){
    	            return new Result(810, "旧报告不存在", null);//重命名文件不存在
    	        }
    	        oldfile.renameTo(newfile);  
    		}
    		report.setTitle(title);
    		report.setUpdateTime(NumUtil.GetNowDate("yyyy-MM-dd HH:mm:ss"));
    		report.setFybm(fybm.startsWith(";")?fybm.substring(1):fybm);
    		report.setFymc(fymc.startsWith(";")?fymc.substring(1):fymc);
    		report.setType(type);
    		report.setZsType(zsType);
        	report.setYear(year);
        	report.setTimeDimension(timeDimension);
    		if(file != null) {
    			String oldTitle = report.getTitle();
        		deleteFile(oldTitle);
        		uploadFile(file,title);
        		//report.setPath(path);
    		}
    		reportService.save(report);
    		return new Result(200, "修改报告成功", null);
    	}else {
    		return new Result(803, "修改报告失败", null);
    	}

	}
    
    @RequestMapping("/delete")
    @ResponseBody
    public Result delete(int id){
    	List<Report> list = reportService.findById(id);
    	if(list.size()>0) {
    		Report report = list.get(0);
    		deleteFile(report.getTitle());
    	}else {
    		return new Result(807,"旧报告不存在",null);
    	}
    	boolean a = reportService.delete(id);
    	if(a) {
    		return new Result(200,"删除报告成功",null);
		}else {
			return new Result(804,"删除报告失败",null);
		}
    }

    @CrossOrigin
    @RequestMapping(value ="/getReportByFybm", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Result getReportByIp(String fybm){
		List list = reportService.findByFybm(fybm);
    	if(list.size()>0) {
    		return new Result(200,"获取报告信息成功",list);
    	}else {
    		return new Result(805,"暂无报告",null);
    	}
    }
    
    @CrossOrigin
    @RequestMapping(value ="/updateReportCount", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Result updateGonggaoStatus(int id){
    	List<Report> list = reportService.findById(id);
    	if(list.size()>0) {
    		Report report = list.get(0);
    		report.setCount(report.getCount()+1);
    		reportService.save(report);
    		return new Result(200,"更新报告信息成功",list);
    	}else {
    		return new Result(806,"获取报告信息失败",null);
    	}
    }
    
    @RequestMapping(value = "/page", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Map<String,Object> page(@RequestParam(value = "limit", required = false, defaultValue = "10") String limit,
			@RequestParam(value = "offset", required = false, defaultValue = "0") String offset,Report report) {
    	Sort sort = new Sort(Direction.DESC,"id");
    	Pageable pageable = PageRequest.of(Integer.valueOf(offset)/Integer.valueOf(limit), Integer.valueOf(limit), sort);
		PageInfo<Report> pageInfo = reportService.getPageByParam(report.getTitle()==null?"":report.getTitle(), report.getFymc()==null?"":report.getFymc(),report.getType()==null?"":report.getType(), pageable);
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("count", pageInfo.getRecords());
		result.put("queryData", pageInfo.getRows());
		return result;
    }
    
    @CrossOrigin
    @RequestMapping(value = "/preview/*", method = RequestMethod.GET)
   	public void pdfStreamHandler(HttpServletRequest request, HttpServletResponse response,Report report) {
    	String path = request.getServletPath().substring(request.getServletPath().lastIndexOf("/")+1);
    	String filePath = PropertiesUtil.getProperty("reportPath")+path;
		File file = new File(filePath);
   		if (file.exists()) {
   			byte[] data = null;
   			FileInputStream input=null;
   			try {
   				input= new FileInputStream(file);
   				data = new byte[input.available()];
   				input.read(data);
   				response.getOutputStream().write(data);
   			} catch (Exception e) {
   				e.printStackTrace();
   			}finally{
   				try {
   					if(input!=null){
   						input.close();
   					}
   				} catch (IOException e) {
   					e.printStackTrace();
   				}
   			}
   		}
   	}
   
    @Autowired 
    TFypzService tFypzService; 
    @Autowired
    DocumentHandler documentHandler;
    @RequestMapping(value = "/produce", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Result produce(HttpServletRequest request,HttpServletResponse response,String type,String year,String timeDimension,String fybms,String fymcs) {
    	Map map = new HashMap();
    	System.out.println("开始生成："+NumUtil.GetNowDate("yyyy-MM-dd HH:mm:ss"));
    	String reportPath =  PropertiesUtil.getProperty("reportPath");
    	String templatePath =  reportPath+"template/";
    	String templateName = "";
    	String templateType = "";
    	String searchType= "";
    	if(type.equals("年报")) {
    		templateType = "year";
    		searchType= "year";
    	}else if(type.equals("半年报")) {
    		templateType = "half";
    		if(timeDimension.equals("上半年")) {
    			searchType = "up";
    		}else if(timeDimension.equals("下半年")) {
    			searchType = "down";
    		}
    	}
    	String kind = "";
    	String reportName = "";
    	//ids = "O60";
    	//ids = "860,310,350,390,110,120,780,810,550,5B0,IA0,K20,O50,S10,N10,L10,C10,C60,CE0,910,A60,BC0,D10,J30";
    	String[] fybmList = fybms.split(";");
    	String[] fymcList = fymcs.split(";");
     	for(int i=0;i<fybmList.length;i++) {
    		if(fybmList[i].length()!=3) {
    			continue;
    		}
    		String fymc = fymcList[i];
    		/*if(fybmList[i].equals("ZZ0")) {//全国报告			
    			templateName =  PropertiesUtil.getProperty("templateName.quanguo."+templateType);
    			kind = "高";
    			if(type.equals("半年报")) {
    				reportName = year +"年"+ timeDimension + fymc + "法院司法综合指数分析评估报告.doc";
    			}else {
    				reportName = year +"年"+ fymc + "法院司法综合指数分析评估报告.doc";
    			}
    			documentHandler.createDoc(kind,templatePath,templateName,reportPath,reportName,fymc,searchType,year,fybmList[i]);// 生成报告
    		}else */if(fybmList[i].substring(1).equals("00")) {//高院报告
    			kind = "高";
    			if(fybmList[i].startsWith("1")||fybmList[i].startsWith("2")||fybmList[i].startsWith("M")||fybmList[i].startsWith("9")) {
    				templateName =  PropertiesUtil.getProperty("templateName.gaoji.zhixia.spzx."+templateType);
    				if(type.equals("半年报")) {
        				reportName = year +"年"+ timeDimension + NumUtil.toSheng(fymc) + "法院审判执行运行情况评估报告.doc";
        			}else {
        				reportName = year +"年"+ NumUtil.toSheng(fymc) + "法院审判执行运行情况评估报告.doc";
        			}
    				//documentHandler.createDoc(kind,templatePath,templateName,reportPath,reportName,fymc,searchType,year,fybmList[i]);// 生成报告
    				saveReport(reportName,fymc,fybmList[i],type,year,timeDimension,"审判执行");
    				templateName =  PropertiesUtil.getProperty("templateName.gaoji.zhixia.jjsh."+templateType);
    				if(type.equals("半年报")) {
        				reportName ="基于司法大数据的" + year +"年"+ timeDimension + NumUtil.toSheng(fymc) + "经济社会运行情况评估报告.doc";
        			}else {
        				reportName ="基于司法大数据的" +  year +"年"+ NumUtil.toSheng(fymc) + "经济社会运行情况评估报告.doc";
        			}
    				
    				//documentHandler.createDoc(kind,templatePath,templateName,reportPath,reportName,fymc,searchType,year,fybmList[i]);// 生成报告
    				saveReport(reportName,fymc,fybmList[i],type,year,timeDimension,"社会治理");
    			}else {
    				templateName =  PropertiesUtil.getProperty("templateName.gaoji.spzx."+templateType);
    				if(type.equals("半年报")) {
        				reportName = year +"年"+ timeDimension + NumUtil.toSheng(fymc) + "法院审判执行运行情况评估报告.doc";
        			}else {
        				reportName = year +"年"+ NumUtil.toSheng(fymc) + "法院审判执行运行情况评估报告.doc";
        			}
    				//documentHandler.createDoc(kind,templatePath,templateName,reportPath,reportName,fymc,searchType,year,fybmList[i]);// 生成前半部分报告
    				saveReport(reportName,fymc,fybmList[i],type,year,timeDimension,"审判执行");
    				templateName =  PropertiesUtil.getProperty("templateName.gaoji.jjsh."+templateType);
    				if(type.equals("半年报")) {
        				reportName ="基于司法大数据的" + year +"年"+ timeDimension + NumUtil.toSheng(fymc) + "经济社会运行情况评估报告.doc";
        			}else {
        				reportName ="基于司法大数据的" +  year +"年"+ NumUtil.toSheng(fymc) + "经济社会运行情况评估报告.doc";
        			}
    				//documentHandler.createDoc(kind,templatePath,templateName,reportPath,reportName,fymc,searchType,year,fybmList[i]);// 生成后半部分报告
    				saveReport(reportName,fymc,fybmList[i],type,year,timeDimension,"社会治理");
			}
    			
    		}else if(fybmList[i].substring(2).equals("0")) {//中院报告
    			kind = "中";
    			if(fybmList[i].startsWith("1")||fybmList[i].startsWith("2")||fybmList[i].startsWith("M")||fybmList[i].startsWith("9")) {
    				templateName =  PropertiesUtil.getProperty("templateName.zhongji.zhixia.spzx."+templateType);//直辖市中院
    				if(type.equals("半年报")) {
        				reportName = year +"年"+ timeDimension + NumUtil.toSheng(fymc) + "审判执行运行情况评估报告.doc";
        			}else {
        				reportName = year +"年"+ NumUtil.toSheng(fymc) + "审判执行运行情况评估报告.doc";
        			}
    				List fyjcList = tFypzService.getFyjc(fybmList[i]);
            		if(fyjcList.size()>0) {
            			fymc = fyjcList.get(0).toString();
            			if(type.equals("半年报")) {
            				reportName = year +"年"+ timeDimension+ fymc + "审判执行运行情况评估报告.doc";
            			}else {
            				reportName = year +"年"+ fymc + "审判执行运行情况评估报告.doc";
            			}		
            		}
    				//documentHandler.createDoc(kind,templatePath,templateName,reportPath,reportName,fymc,searchType,year,fybmList[i]);
    				saveReport(reportName,fymc,fybmList[i],type,year,timeDimension,"审判执行");
    			}else {//普通中院
    				templateName =  PropertiesUtil.getProperty("templateName.zhongji.spzx."+templateType);
    				if(type.equals("半年报")) {
        				reportName = year +"年"+ timeDimension + NumUtil.toSheng(fymc) + "审判执行运行情况评估报告.doc";
        			}else {
        				reportName = year +"年"+ NumUtil.toSheng(fymc) + "审判执行运行情况评估报告.doc";
        			}
    				List fyjcList = tFypzService.getFyjc(fybmList[i]);
            		if(fyjcList.size()>0) {
            			fymc = fyjcList.get(0).toString();
            			if(type.equals("半年报")) {
            				reportName = year +"年"+ timeDimension+ fymc + "审判执行运行情况评估报告.doc";
            			}else {
            				reportName = year +"年"+ fymc + "审判执行运行情况评估报告.doc";
            			}		
            		}
    				//documentHandler.createDoc(kind,templatePath,templateName,reportPath,reportName,fymc,searchType,year,fybmList[i]);// 生成报告
    				saveReport(reportName,fymc,fybmList[i],type,year,timeDimension,"审判执行");

            		templateName =  PropertiesUtil.getProperty("templateName.zhongji.jjsh."+templateType);//直辖市中院
            		if(type.equals("半年报")) {
        				reportName ="基于司法大数据的" + year +"年"+ timeDimension + fymc.replace("法院", "") + "经济社会运行情况评估报告.doc";
        			}else {
        				reportName ="基于司法大数据的" +  year +"年"+ fymc.replace("法院", "") + "经济社会运行情况评估报告.doc";
        			}
            		if(fyjcList.size()>0) {
            			fymc = fyjcList.get(0).toString();
            			if(type.equals("半年报")) {
            				reportName = year +"年"+ timeDimension+ fymc.replace("法院", "") + "经济社会运行情况评估报告.doc";
            			}else {
            				reportName = year +"年"+ fymc.replace("法院", "") + "经济社会运行情况评估报告.doc";
            			}		
            		}
            		//documentHandler.createDoc(kind,templatePath,templateName,reportPath,reportName,fymc,searchType,year,fybmList[i]);// 生成报告
            		saveReport(reportName,fymc,fybmList[i],type,year,timeDimension,"社会治理");
    			}
    		}else {//基层院报告
    			kind = "辖";
    			templateName =  PropertiesUtil.getProperty("templateName.jiceng.spzx."+templateType);
    			if(type.equals("半年报")) {
    				reportName = year +"年"+ timeDimension+ fymc + "审判执行运行情况评估报告.doc";
    			}else {
    				reportName = year +"年"+ fymc + "审判执行运行情况评估报告.doc";
    			}
    			//documentHandler.createDoc(kind,templatePath,templateName,reportPath,reportName,fymc,searchType,year,fybmList[i]);// 生成报告
    			saveReport(reportName,fymc,fybmList[i],type,year,timeDimension,"审判执行");
    			templateName =  PropertiesUtil.getProperty("templateName.jiceng.jjsh."+templateType);
    			if(type.equals("半年报")) {
    				reportName = year +"年"+ timeDimension+ fymc.replace("人民法院", "") + "经济社会运行情况评估报告.doc";
    			}else {
    				reportName = year +"年"+ fymc.replace("人民法院", "") + "经济社会运行情况评估报告.doc";
    			}
    			//documentHandler.createDoc(kind,templatePath,templateName,reportPath,reportName,fymc,searchType,year,fybmList[i]);// 生成报告
    			saveReport(reportName,fymc,fybmList[i],type,year,timeDimension,"社会治理");
    		}
     	}
     	return new Result(200,"报告制作成功",null);

	}
    
    public void saveReport(String reportName,String fymc,String fybm,String type,String year,String timeDimension,String zsType) {
    	List<Report> list = reportService.findByParam(fybm, year, type, zsType, timeDimension);
    	Report report = null;
    	if(list.size()>0) {
    		report = list.get(0);
        	report.setUpdateTime(NumUtil.GetNowDate("yyyy-MM-dd HH:mm:ss"));
    	}else {
    		report = new Report();
        	report.setFybm(fybm);
    		report.setFymc(fymc);
    		report.setFybm(fybm.startsWith(";")?fybm.substring(1):fybm);
    		report.setFymc(fymc.startsWith(";")?fymc.substring(1):fymc);
        	report.setTitle(reportName);
        	report.setCount(0);
        	report.setType(type);
        	report.setYear(year);
        	report.setTimeDimension(timeDimension);
        	report.setProduceType("auto");
        	report.setZsType(zsType);
        	//report.setPath(path);
        	report.setUpdateTime(NumUtil.GetNowDate("yyyy-MM-dd HH:mm:ss"));
    	}
    	reportService.save(report);
    }
    
    @RequestMapping(value = "/reProduce", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Result reProduce(HttpServletRequest request,HttpServletResponse response,int id,String type,String year,String timeDimension,String fybms,String fymcs) {
    	Map map = new HashMap();
    	System.out.println("开始生成："+NumUtil.GetNowDate("yyyy-MM-dd HH:mm:ss"));
    	String reportPath =  PropertiesUtil.getProperty("reportPath");
    	String templatePath =  reportPath+"template/";
    	String templateName = "";
    	String templateType = "";
    	String searchType= "";
    	if(type.equals("年报")) {
    		templateType = "year";
    		searchType= "year";
    	}else if(type.equals("半年报")) {
    		templateType = "half";
    		if(timeDimension.equals("上半年")) {
    			searchType = "up";
    		}else if(timeDimension.equals("下半年")) {
    			searchType = "down";
    		}
    	}
    	String kind = "";
    	String reportName = "";
    	//ids = "O60";
    	//ids = "860,310,350,390,110,120,780,810,550,5B0,IA0,K20,O50,S10,N10,L10,C10,C60,CE0,910,A60,BC0,D10,J30";
    	String[] fybmList = fybms.split(";");
    	String[] fymcList = fymcs.split(";");
     	for(int i=0;i<fybmList.length;i++) {
    		if(fybmList[i].length()!=3) {
    			continue;
    		}
    		String fymc = fymcList[i];
    		/*if(fybmList[i].equals("ZZ0")) {//全国报告			
    			templateName =  PropertiesUtil.getProperty("templateName.quanguo."+templateType);
    			kind = "高";
    			if(type.equals("半年报")) {
    				reportName = year +"年"+ timeDimension + fymc + "法院司法综合指数分析评估报告.doc";
    			}else {
    				reportName = year +"年"+ fymc + "法院司法综合指数分析评估报告.doc";
    			}
    			documentHandler.createDoc(kind,templatePath,templateName,reportPath,reportName,fymc,searchType,year,fybmList[i]);// 生成报告
    		}else */if(fybmList[i].substring(1).equals("00")) {//高院报告
    			kind = "高";
    			if(fybmList[i].startsWith("1")||fybmList[i].startsWith("2")||fybmList[i].startsWith("M")||fybmList[i].startsWith("9")) {
    				templateName =  PropertiesUtil.getProperty("templateName.gaoji.zhixia.spzx."+templateType);
    				if(type.equals("半年报")) {
        				reportName = year +"年"+ timeDimension + NumUtil.toSheng(fymc) + "法院审判执行运行情况评估报告.doc";
        			}else {
        				reportName = year +"年"+ NumUtil.toSheng(fymc) + "法院审判执行运行情况评估报告.doc";
        			}
    				//documentHandler.createDoc(kind,templatePath,templateName,reportPath,reportName,fymc,searchType,year,fybmList[i]);// 生成报告
    				saveReport(reportName,fymc,fybmList[i],type,year,timeDimension,"审判执行");
    				templateName =  PropertiesUtil.getProperty("templateName.gaoji.zhixia.jjsh."+templateType);
    				if(type.equals("半年报")) {
        				reportName ="基于司法大数据的" + year +"年"+ timeDimension + NumUtil.toSheng(fymc) + "经济社会运行情况评估报告.doc";
        			}else {
        				reportName ="基于司法大数据的" +  year +"年"+ NumUtil.toSheng(fymc) + "经济社会运行情况评估报告.doc";
        			}
    				//documentHandler.createDoc(kind,templatePath,templateName,reportPath,reportName,fymc,searchType,year,fybmList[i]);// 生成报告
    			}else {
    				templateName =  PropertiesUtil.getProperty("templateName.gaoji.spzx."+templateType);
    				if(type.equals("半年报")) {
        				reportName = year +"年"+ timeDimension + NumUtil.toSheng(fymc) + "法院审判执行运行情况评估报告.doc";
        			}else {
        				reportName = year +"年"+ NumUtil.toSheng(fymc) + "法院审判执行运行情况评估报告.doc";
        			}
    				//documentHandler.createDoc(kind,templatePath,templateName,reportPath,reportName,fymc,searchType,year,fybmList[i]);// 生成前半部分报告
    				templateName =  PropertiesUtil.getProperty("templateName.gaoji.jjsh."+templateType);
    				if(type.equals("半年报")) {
        				reportName ="基于司法大数据的" + year +"年"+ timeDimension + NumUtil.toSheng(fymc) + "经济社会运行情况评估报告.doc";
        			}else {
        				reportName ="基于司法大数据的" +  year +"年"+ NumUtil.toSheng(fymc) + "经济社会运行情况评估报告.doc";
        			}
    				//documentHandler.createDoc(kind,templatePath,templateName,reportPath,reportName,fymc,searchType,year,fybmList[i]);// 生成后半部分报告
			}
    			
    		}else if(fybmList[i].substring(2).equals("0")) {//中院报告
    			kind = "中";
    			if(fybmList[i].startsWith("1")||fybmList[i].startsWith("2")||fybmList[i].startsWith("M")||fybmList[i].startsWith("9")) {
    				templateName =  PropertiesUtil.getProperty("templateName.zhongji.zhixia.spzx."+templateType);//直辖市中院
    				if(type.equals("半年报")) {
        				reportName = year +"年"+ timeDimension + NumUtil.toSheng(fymc) + "审判执行运行情况评估报告.doc";
        			}else {
        				reportName = year +"年"+ NumUtil.toSheng(fymc) + "审判执行运行情况评估报告.doc";
        			}
    				List fyjcList = tFypzService.getFyjc(fybmList[i]);
            		if(fyjcList.size()>0) {
            			fymc = fyjcList.get(0).toString();
            			if(type.equals("半年报")) {
            				reportName = year +"年"+ timeDimension+ fymc + "审判执行运行情况评估报告.doc";
            			}else {
            				reportName = year +"年"+ fymc + "审判执行运行情况评估报告.doc";
            			}		
            		}
    				//documentHandler.createDoc(kind,templatePath,templateName,reportPath,reportName,fymc,searchType,year,fybmList[i]);
    			}else {//普通中院
    				templateName =  PropertiesUtil.getProperty("templateName.zhongji.spzx."+templateType);
    				if(type.equals("半年报")) {
        				reportName = year +"年"+ timeDimension + NumUtil.toSheng(fymc) + "审判执行运行情况评估报告.doc";
        			}else {
        				reportName = year +"年"+ NumUtil.toSheng(fymc) + "审判执行运行情况评估报告.doc";
        			}
    				List fyjcList = tFypzService.getFyjc(fybmList[i]);
            		if(fyjcList.size()>0) {
            			fymc = fyjcList.get(0).toString();
            			if(type.equals("半年报")) {
            				reportName = year +"年"+ timeDimension+ fymc + "审判执行运行情况评估报告.doc";
            			}else {
            				reportName = year +"年"+ fymc + "审判执行运行情况评估报告.doc";
            			}		
            		}
    				//documentHandler.createDoc(kind,templatePath,templateName,reportPath,reportName,fymc,searchType,year,fybmList[i]);// 生成报告
    				
            		templateName =  PropertiesUtil.getProperty("templateName.zhongji.jjsh."+templateType);//直辖市中院
            		if(type.equals("半年报")) {
        				reportName ="基于司法大数据的" + year +"年"+ timeDimension + fymc.replace("法院", "") + "经济社会运行情况评估报告.doc";
        			}else {
        				reportName ="基于司法大数据的" +  year +"年"+ fymc.replace("法院", "") + "经济社会运行情况评估报告.doc";
        			}
            		if(fyjcList.size()>0) {
            			fymc = fyjcList.get(0).toString();
            			if(type.equals("半年报")) {
            				reportName = year +"年"+ timeDimension+ fymc.replace("法院", "") + "经济社会运行情况评估报告.doc";
            			}else {
            				reportName = year +"年"+ fymc.replace("法院", "") + "经济社会运行情况评估报告.doc";
            			}		
            		}
            		//documentHandler.createDoc(kind,templatePath,templateName,reportPath,reportName,fymc,searchType,year,fybmList[i]);// 生成报告
    			}
    		}else {//基层院报告
    			kind = "辖";
    			templateName =  PropertiesUtil.getProperty("templateName.jiceng.spzx."+templateType);
    			if(type.equals("半年报")) {
    				reportName = year +"年"+ timeDimension+ fymc + "审判执行运行情况评估报告.doc";
    			}else {
    				reportName = year +"年"+ fymc + "审判执行运行情况评估报告.doc";
    			}
    			//documentHandler.createDoc(kind,templatePath,templateName,reportPath,reportName,fymc,searchType,year,fybmList[i]);// 生成报告
    			templateName =  PropertiesUtil.getProperty("templateName.jiceng.jjsh."+templateType);
    			if(type.equals("半年报")) {
    				reportName = year +"年"+ timeDimension+ fymc.replace("人民法院", "") + "经济社会运行情况评估报告.doc";
    			}else {
    				reportName = year +"年"+ fymc.replace("人民法院", "") + "经济社会运行情况评估报告.doc";
    			}
    			//documentHandler.createDoc(kind,templatePath,templateName,reportPath,reportName,fymc,searchType,year,fybmList[i]);// 生成报告
    		}
     	}
     	List<Report> list = reportService.findById(id);
     	if(list.size()>0) {
     		Report report = list.get(0);
     		report.setUpdateTime(NumUtil.GetNowDate("yyyy-MM-dd HH:mm:ss"));
     		reportService.save(report);
     	}
     	return new Result(200,"重新生成报告",null);

	}
    
    @RequestMapping(value = "/getMapData", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Result getMapData(String fybm, String type,String year,String timeDimension) {
    	String date = "";
    	if(type.equals("年报")) {
    		type = "year";
    		date = year;
    	}else if(type.equals("半年报")) {
    		if(timeDimension.equals("上半年")) {
    			type = "up";
    		}else {
    			type = "down";
    		}
    		date = year;
    	}else if(type.equals("月报")) {
    		type="month";
    		date=year+"-" + NumUtil.getMonthNum(timeDimension);
    	}
    	String result = reportService.getMapData(fybm, type, date);
    	return new Result(200,"获取地图数据成功",result);
    }
    
    @ResponseBody
	@RequestMapping(value = "/loadpic", method = RequestMethod.POST)
    public Map<String, Object> loadpic(HttpServletRequest request, HttpServletResponse response) {
        String fybm = request.getParameter("fybm");
        String year = request.getParameter("year");
        String timeDimension = request.getParameter("timeDimension");
        Map<String, Object> result = new HashMap<String, Object>();
        String[] picData1 = request.getParameter("data1").split(",");
        String[] picData2 = request.getParameter("data2").split(",");
        /*String[] picData3 = request.getParameter("data3").split(",");
        String[] picData4 = request.getParameter("data4").split(",");
        String[] picData5 = request.getParameter("data5").split(",");
        String[] picData6 = request.getParameter("data6").split(",");
        String[] picData7 = request.getParameter("data7").split(",");
        String[] picData8 = request.getParameter("data8").split(",");
        String[] picData9 = request.getParameter("data9").split(",");
        String[] picData10 = request.getParameter("data10").split(",");
        String[] picData11 = request.getParameter("data11").split(",");
        String[] picData12 = request.getParameter("data12").split(",");
        String[] picData13 = request.getParameter("data13").split(",");
        String[] picData14 = request.getParameter("data14").split(",");
        String[] picData15 = request.getParameter("data15").split(",");
        String[] picData16 = request.getParameter("data16").split(",");*/
 
        try {                                                                                                                                                                                                                      
            byte[] n1 = new Base64().decode(picData1[1]);
            byte[] n2 = new Base64().decode(picData2[1]);
            /*byte[] n3 = new Base64().decode(picData3[1]);
            byte[] n4 = new Base64().decode(picData4[1]);
            byte[] n5 = new Base64().decode(picData5[1]);
            byte[] n6 = new Base64().decode(picData6[1]);
            byte[] n7 = new Base64().decode(picData7[1]);
            byte[] n8 = new Base64().decode(picData8[1]);
            byte[] n9 = new Base64().decode(picData9[1]);
            byte[] n10 = new Base64().decode(picData10[1]);
            byte[] n11 = new Base64().decode(picData11[1]);
            byte[] n12 = new Base64().decode(picData12[1]);
            byte[] n13 = new Base64().decode(picData13[1]);
            byte[] n14 = new Base64().decode(picData14[1]);
            byte[] n15 = new Base64().decode(picData15[1]);
            byte[] n16 = new Base64().decode(picData16[1]);*/
            
            
            String reportPath =  PropertiesUtil.getProperty("reportPath");
            String imgPath = reportPath+"img/"+fybm+year+timeDimension;
            File fp = new File(imgPath);
            if (!fp.exists()) {
                fp.mkdirs();
            }            
            OutputStream out = new FileOutputStream(new File(imgPath+"1.png"));  
            out.write(n1); 
            out.flush();
            out.close();
            
            out = new FileOutputStream(new File(imgPath+"2.png"));  
            out.write(n2); 
            out.flush();
            out.close();
            /*out = new FileOutputStream(new File(reportRootPath+"img/"+picpath+"/"+"3.png"));  
            out.write(n3); 
            out.flush();
            out.close();
            out = new FileOutputStream(new File(reportRootPath+"img/"+picpath+"/"+"4.png"));  
            out.write(n4); 
            out.flush();
            out.close();
            out = new FileOutputStream(new File(reportRootPath+"img/"+picpath+"/"+"5.png"));  
            out.write(n5); 
            out.flush();
            out.close();
            out = new FileOutputStream(new File(reportRootPath+"img/"+picpath+"/"+"6.png"));  
            out.write(n6); 
            out.flush();
            out.close();
            out = new FileOutputStream(new File(reportRootPath+"img/"+picpath+"/"+"7.png"));  
            out.write(n7); 
            out.flush();
            out.close();
            out = new FileOutputStream(new File(reportRootPath+"img/"+picpath+"/"+"8.png"));  
            out.write(n8); 
            out.flush();
            out.close();
            out = new FileOutputStream(new File(reportRootPath+"img/"+picpath+"/"+"9.png"));  
            out.write(n9); 
            out.flush();
            out.close();
            out = new FileOutputStream(new File(reportRootPath+"img/"+picpath+"/"+"10.png"));  
            out.write(n10); 
            out.flush();
            out.close();
            out = new FileOutputStream(new File(reportRootPath+"img/"+picpath+"/"+"11.png"));  
            out.write(n11); 
            out.flush();
            out.close();
            out = new FileOutputStream(new File(reportRootPath+"img/"+picpath+"/"+"12.png"));  
            out.write(n12); 
            out.flush();
            out.close();
            out = new FileOutputStream(new File(reportRootPath+"img/"+picpath+"/"+"13.png"));  
            out.write(n13); 
            out.flush();
            out.close();
            out = new FileOutputStream(new File(reportRootPath+"img/"+picpath+"/"+"14.png"));  
            out.write(n14); 
            out.flush();
            out.close();
            out = new FileOutputStream(new File(reportRootPath+"img/"+picpath+"/"+"15.png"));  
            out.write(n15); 
            out.flush();
            out.close();
            out = new FileOutputStream(new File(reportRootPath+"img/"+picpath+"/"+"16.png"));  
            out.write(n16); 
            out.flush();
            out.close();*/
            
            result.put("code", 200);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
                        
 
        return result;
        
    }
}
